package com.ruoyi.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.security.domain.AfDeviceCamera;
import com.ruoyi.security.domain.AfElevator;
import com.ruoyi.security.domain.AfElevatorInfo;
import com.ruoyi.security.domain.vo.AfElevatorOnlineRateBPVo;
import com.ruoyi.security.domain.vo.ElevatorRealDataVo;
import com.ruoyi.security.domain.vo.ElevatorStatusVo;
import com.ruoyi.security.elevator.util.ElevatorDataValue;
import com.ruoyi.security.mapper.AfDeviceCameraMapper;
import com.ruoyi.security.mapper.AfElevatorMapper;
import com.ruoyi.security.service.IAfElevatorInfoService;
import com.ruoyi.security.service.IAfElevatorService;
import com.ruoyi.security.service.IElevatorSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 电梯管理Service业务层处理
 *
 * @author Felix
 * @date 2021-10-12
 */
@Service
public class AfElevatorServiceImpl implements IAfElevatorService {
    @Autowired
    private AfElevatorMapper afElevatorMapper;

    @Autowired
    private IAfElevatorInfoService infoService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private AfDeviceCameraMapper afDeviceCameraMapper;

    @Autowired
    private IElevatorSendService sendService;

    /**
     * 查询电梯管理
     *
     * @param id 电梯管理ID
     * @return 电梯管理
     */
    @Override
    public AfElevator selectAfElevatorById(String id) {
        AfElevator result = afElevatorMapper.selectAfElevatorById(id);
        if(result != null){
            if(StringUtils.isNotEmpty(result.getCameraId())){
                AfDeviceCamera camera = afDeviceCameraMapper.selectAfDeviceCameraById(result.getCameraId());
                //设置视频地址
                if(camera != null){
                    result.setPcUrl(camera.getPcUrl());
                }
            }

        }

        return result;
    }

    /**
     * 查询电梯管理列表
     *
     * @param afElevator 电梯管理
     * @return 电梯管理
     */
    @Override
    public List<AfElevator> selectAfElevatorList(AfElevator afElevator) {
        return afElevatorMapper.selectAfElevatorList(afElevator);
    }


    /**
     * 新增电梯管理
     *
     * @param afElevator 电梯管理
     * @return 结果
     */
    @Override
    public int insertAfElevator(AfElevator afElevator) {
        afElevator.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        afElevator.setCreateTime(DateUtils.getNowDate());
        afElevator.setCreateBy(SecurityUtils.getUsername());
        return afElevatorMapper.insertAfElevator(afElevator);
    }

    /**
     * 修改电梯管理
     *
     * @param afElevator 电梯管理
     * @return 结果
     */
    @Override
    public int updateAfElevator(AfElevator afElevator) {

        afElevator.setUpdateTime(DateUtils.getNowDate());
        afElevator.setUpdateBy(SecurityUtils.getUsername());
        return afElevatorMapper.updateAfElevator(afElevator);
    }

    /**
     * 修改电梯管理配置
     *
     * @param afElevator 电梯管理配置
     * @return 结果
     */
    @Override
    public int updateAfElevatorConfig(AfElevator afElevator) {
        boolean flag = false;
        if(StringUtils.isNotEmpty(afElevator.getElevStatus()) && afElevator.getElevStatus().equals("1")){
            //未联网到联网，发送websocket命令
            AfElevator afElevator1 = afElevatorMapper.selectAfElevatorById(afElevator.getId());
            if(!afElevator1.getElevStatus().equals("1")){
                flag= true;
            }
        }
        int result = afElevatorMapper.updateAfElevator(afElevator);

        if(flag){
            sendService.sendGetElevatorRealData("1");
        }
        return result;
    }
    /**
     * 批量删除电梯管理
     *
     * @param ids 需要删除的电梯管理ID
     * @return 结果
     */
    @Override
    public int deleteAfElevatorByIds(String[] ids) {
        return afElevatorMapper.deleteAfElevatorByIds(ids);
    }

    /**
     * 删除电梯管理信息
     *
     * @param id 电梯管理ID
     * @return 结果
     */
    @Override
    public int deleteAfElevatorById(String id) {
        return afElevatorMapper.deleteAfElevatorById(id);
    }

    @Override
    public ElevatorRealDataVo getElevatorRealData(String id) {
        ElevatorRealDataVo realDataVo = null;
        AfElevatorInfo info = null;
        AfElevator elevator = selectAfElevatorById(id);
        if (elevator != null && StringUtils.isNotBlank(elevator.getElevId())) {
            AfElevatorInfo query = new AfElevatorInfo();
            query.setElevId(elevator.getElevId());
            List<AfElevatorInfo> list = infoService.selectAfElevatorInfoList(query);
            if (list.size() > 0) {
                info = list.get(0);
               String sd = redisService.getCacheObject(info.getRegisterCode());
                realDataVo = JSON.parseObject(sd, ElevatorRealDataVo.class);
                if(realDataVo != null){
                    if(realDataVo.getElevatorInfo() != null){
                        realDataVo.getElevatorInfo().setPcUrl(elevator.getPcUrl());
                    }

                }else {
                    realDataVo = new ElevatorRealDataVo();
                    realDataVo.setElevatorInfo(info);
                }
                return realDataVo;
            }
        }else {
            realDataVo = new ElevatorRealDataVo();
            info = new AfElevatorInfo();
            BeanUtil.copyProperties(elevator,info);
            realDataVo.setElevatorInfo(info);
        }
        return realDataVo;
    }

    @Override
    public List<AfElevator> getElevatorErrorList() {

        List<AfElevator> resultList = new ArrayList<>();
        for (ElevatorStatusVo statusVo : ElevatorDataValue.errorList) {
            AfElevator elevator = afElevatorMapper.selectAfElevatorById(statusVo.getId());
            elevator.setErrorTime(statusVo.getDate());
            resultList.add(elevator);
        }

        return resultList;
    }

    @Override
    public AfElevatorOnlineRateBPVo selectAfElevatorOnlineRate(AfElevator afElevator) {
        AfElevatorOnlineRateBPVo onlineRateBPVo = new AfElevatorOnlineRateBPVo();
        //return R.ok(afElevatorService.updateAfElevator(afElevator));
        // 统计，
        List<AfElevator> afElevatorList = this.selectAfElevatorList(afElevator);
        int huotiCount =0;
        int ketiCount =0;
        int weihuCount =0;
        int offlineCount=0;
        int onlineCount=0;
        int jianxiuCount=0;
        int faultCount =0;
        int count = afElevatorList.size();
        for(AfElevator elevator:afElevatorList){
            if(elevator.getElevatorName().endsWith("货梯")){
                huotiCount ++;
            }else{
                ketiCount ++;
            }
            if(StringUtils.isNull(elevator.getIsOnline()) || elevator.getIsOnline().equals("1")){
                onlineCount ++;
            }else {
                offlineCount ++;
            }
            if(elevator.getState().equals("检修")){
                jianxiuCount++;
            }else if(elevator.getState().equals("故障")){
                faultCount++;
            }

        }
        onlineRateBPVo.setOnlineRate(onlineCount*100/count);
        onlineRateBPVo.setGoodsTotal(huotiCount);
        onlineRateBPVo.setGuestTotal(ketiCount);
        onlineRateBPVo.setMaintainTotal(weihuCount);
        onlineRateBPVo.setOfflineRate(offlineCount*100/count);
        onlineRateBPVo.setCheckRate(jianxiuCount*100/count);
        onlineRateBPVo.setFaltRate(faultCount*100/count);
//        AfElevatorOnlineRateBPVo.FaltBPVo faltBPVo = new AfElevatorOnlineRateBPVo.FaltBPVo();
//        faltBPVo.setType("货梯");
//        faltBPVo.setLocation("A区5栋08号");
//        faltBPVo.setTotal(4);
//        faltBPVo.setFaltRate(8);
//        onlineRateBPVo.getFalts().add(faltBPVo);
        //今日故障
        List<AfElevatorOnlineRateBPVo.FaltBPVo> falts = afElevatorMapper.selectFaltsByToday();
        onlineRateBPVo.setFalts(falts);

        //运行时长
        Map runTimeMap = onlineRateBPVo.getRunTimeMap();
        runTimeMap.put("00:00",0);
        runTimeMap.put("03:00",50);
        runTimeMap.put("06:00",400);
        runTimeMap.put("09:00",700);
        runTimeMap.put("12:00",400);
        runTimeMap.put("15:00",500);
        runTimeMap.put("18:00",700);
        runTimeMap.put("21:00",200);
        runTimeMap.put("24:00",0);
        //运行次数
        Map runNumberMap = onlineRateBPVo.getRunNumberMap();
        runNumberMap.put("00:00",0);
        runNumberMap.put("03:00",5);
        runNumberMap.put("06:00",40);
        runNumberMap.put("09:00",70);
        runNumberMap.put("12:00",40);
        runNumberMap.put("15:00",50);
        runNumberMap.put("18:00",70);
        runNumberMap.put("21:00",20);
        runNumberMap.put("24:00",0);

        return onlineRateBPVo;
    }
}
